home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 646 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: solon.com!not-for-mail
  2. From: brianmcg@interaccess.com (Brian V. McGroarty)
  3. Newsgroups: comp.lang.c.moderated,comp.std.c
  4. Subject: Re: 'h' modifier in printf
  5. Date: 24 Mar 1996 11:49:12 -0600
  6. Organization: Internet Squire
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4j41uo$nu7@solutions.solon.com>
  10. References: <4j06rd$82n@solutions.solon.com>
  11. Reply-To: brianmcg@interaccess.com
  12. NNTP-Posting-Host: solutions.solon.com
  13. X-Newsreader: Internet Squire 1.20
  14.  
  15.  
  16.  
  17.  
  18. Holden P wrote:
  19. >printf("Vanilla %X\nWith h  %hX\n", -1, -1);
  20.  
  21. >says
  22.  
  23. >Vanilla FFFFFFFF
  24. >With h  FFFF
  25.  
  26.  
  27. You are using a machine/compiler with a 32-bit integer size.  "printf"
  28. follows the default integer size of the machine for its parameters when a
  29. size isn't explicitly given.  A 16-bit integer machine/compiler would print
  30. "FFFF" for both.
  31.  
  32.  
  33.  
  34. >In article <4i801c$455@solutions.solon.com>,
  35. >    tada@athena.mit.edu (Michael J Zehr) writes:
  36.  
  37. > >I was recently asked a question about printf whose answer I couldn't
  38. > >determine by reading K&R2 (and alas the company doesn't have a copy of
  39. > >the standard to refer to).
  40.  
  41. > >The "h" modifier says the corresponding argument will be printed as a
  42. > >short or unsigned short.
  43.  
  44. > >So, given:
  45.  
  46. > >short s;
  47. > >printf("%d", s);
  48. > >printf("%hd", s);
  49.  
  50. > >(Assuming of course that s has been initialized at some point.)
  51.  
  52. > >Can these two ever be different?  I'm aware of course that the short is
  53. > >widened to an int during the function call, but this preserves the
  54. > >value.
  55.  
  56.  
  57. All bits in the top half of a short which is extended to a full int will be
  58. set to the top bit of the original number.  This assumes
  59. that the short isn't the same size as the default "int", which is of course
  60. a portability concern.
  61.  
  62.  
  63.  
  64. ---
  65. Brian Valters McGroarty -- brianmcg@bix.com
  66. phone/fax (847) 439-7714
  67.